﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Collections;

/// <summary>
/// Summary description for BusinessLogic
/// </summary>
namespace Saiket.BLL
{
    public class BusinessLogicLayer
    {
        public string siteDirName = System.Configuration.ConfigurationManager.AppSettings["siteDirName"].ToString();
        string extension = System.Configuration.ConfigurationManager.AppSettings["extension"].ToString();
        DataLogicLayer dll;
        public BusinessLogicLayer()
	    {
            dll = new DataLogicLayer();
		    // TODO: Add constructor logic here
		    //

	    }
        public string GetColudedTagsLinks()
        {
             DataTable dt = dll.GetAllTagInfo();
             int[] MaxMin = dll.GetMaxMinTagCount();
            // int MINQTY =Int32.Parse( dt.Rows[dt.Rows.Count - 1]["TagCount"].ToString());
            // int MAXQTY =Int32.Parse(dt.Rows[0]["TagCount"].ToString());
            int MAXQTY = MaxMin[0]; 
            int MINQTY = MaxMin[1];
             

             
            int max_size = 150; 
            int min_size = 80;

            int spread = MAXQTY - MINQTY;


            if (0 == spread)
            { 
                spread = 1;
            }
            int step = (max_size - min_size)/(spread);

            string taglinks = null;

            foreach (DataRow dr in dt.Rows)
            {
                int countValue = (int)dr["TagCount"];
                int size = min_size + ((countValue - MINQTY) * step);

                string url = siteDirName + "post/tags/" + dr["TagName"] + extension;
                taglinks += "<a style='color:#7D8085; font-size:" + size + "%'  href='" + url + "'>" + dr["TagName"] + "</a> ";
                
            }
            return taglinks;
        }
        public bool IsTagAvail(string tagName)
        {
            DataLogicLayer dll = new DataLogicLayer();
            DataSet ds = dll.RunQuery(DataLogicSQL.IsTagAvailSQL(tagName));
            if (ds.Tables[0].Rows.Count == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        

        public bool SetTagsAfterUpdate(string tags)
        {
            string[] AllTags = tags.Split(',');
            DataLogicLayer dll = new DataLogicLayer();
            
            foreach (string SingleTag in AllTags)
            {
                if (IsTagAvail(SingleTag))
                {
                    dll.UpdateTagCount(SingleTag);
                }
                else
                {
                    dll.InsertANewTag(SingleTag);
                }
            }
            return true;
        }



        public bool SetTagsAfterInsert(string tags)
        {
            string[] AllTags = tags.Split(',');
            DataLogicLayer dll = new DataLogicLayer();
            
            foreach (string SingleTag in AllTags)
            {
                if (IsTagAvail(SingleTag))
                {
                    dll.UpdateTagCount(SingleTag);
                }
                else
                {
                    dll.InsertANewTag(SingleTag);
                }
            }
            return true;
        }
        

        public bool IsUserValid(string username, string password)
        {

            DataSet ds = dll.RunQuery(DataLogicSQL.GetLoginSQL(username, password));
           if (ds.Tables[0].Rows.Count == 1)
           {
               return true;
           }
           else
           {
               return false;   
           }
        }
     
        public void GenerateNewsRSS(string NewsRSSFileName)
        {
            string siteDirName = System.Configuration.ConfigurationManager.AppSettings["siteDirName"].ToString();
            string extension = System.Configuration.ConfigurationManager.AppSettings["extension"].ToString();

            XmlDocument doc = new XmlDocument();
            doc.Load(NewsRSSFileName);

            XmlElement root = doc.DocumentElement;
            XmlNode XMLChannel = root.SelectSingleNode("/rss/channel");
            XMLChannel.RemoveAll();

            XmlElement title = doc.CreateElement("title");
            title.InnerText = "Latest News of Saiket Podder";

            XmlElement link = doc.CreateElement("link");
            link.InnerText = siteDirName + "News/News.aspx";

            XmlElement description = doc.CreateElement("description");
            description.InnerText = "Its a News Feed of Saiket Podder's CyberSpace";

            XMLChannel.AppendChild(title);
            XMLChannel.AppendChild(link);
            XMLChannel.AppendChild(description);

            /*
 GetNewsCommand.CommandText = @"SELECT id, 
                                     NewsTitle,
                                     REPLACE(NewsTitle,' ','-') AS NewsTitleLinks,
                                     DAY(NewsDate) AS date,
                                     MONTH(NewsDate) AS month,
                                     YEAR(NewsDate) AS year,
                                     NewsCaption,
                                     News,
                                     NewsDate,
                                     IsPublished,
                                     PhotoAvail,
                                     DATEDIFF(day, NewsDate, GETDATE()) AS dayDiff,
                                     DATEDIFF(hh,NewsDate, GETDATE()) AS HourDiff
                                     FROM  News
                                     WHERE (IsPublished =@IsPublished)";    
             * 
                           <a href="<%=siteDirName%>News/<%# Eval("month")%>/<%# Eval("date")%>/<%# Eval("year")%>/<%# Eval("id")%>/<%# Eval("NewsTitleLinks")%><%=extension %>"><%# Eval("NewsTitle")%></a>
             */
            DataTable dt = dll.GetNewsTable(true);// ture to get all published news
            foreach (DataRow dr in dt.Rows)
            {
                XmlElement item = doc.CreateElement("item");

                XmlElement ItemTitle = doc.CreateElement("title");
                ItemTitle.InnerText = dr["NewsTitle"].ToString();

                XmlElement ItemLink = doc.CreateElement("link");
                ItemLink.InnerText = siteDirName + "News/" + dr["month"].ToString() + "/" + dr["date"].ToString() + "/" + dr["year"].ToString() + "/" + dr["id"].ToString() + "/" + dr["NewsTitleLinks"].ToString() + extension;

                XmlElement ItemDescription = doc.CreateElement("description");
                ItemDescription.InnerText = dr["News"].ToString();

                item.AppendChild(ItemTitle);
                item.AppendChild(ItemLink);
                item.AppendChild(ItemDescription);
                XMLChannel.AppendChild(item);
            }

            doc.Save(NewsRSSFileName);
        }

        public void GenerateBlogRSS(string BlogRSSFileName)
        {

            string siteDirName = System.Configuration.ConfigurationManager.AppSettings["siteDirName"].ToString();
            string extension = System.Configuration.ConfigurationManager.AppSettings["extension"].ToString();

            XmlDocument doc = new XmlDocument();
            doc.Load(BlogRSSFileName);

            XmlElement root = doc.DocumentElement;
            XmlNode XMLChannel = root.SelectSingleNode("/rss/channel");
            XMLChannel.RemoveAll();

            XmlElement title = doc.CreateElement("title");
            title.InnerText = "Saiket Podder's Blog";

            XmlElement link = doc.CreateElement("link");
            link.InnerText = siteDirName+"Blog/Blog.aspx";

            XmlElement description = doc.CreateElement("description");
            description.InnerText = "Its a Blog Feed of Saiket Podder's CyberSpace";

            XMLChannel.AppendChild(title);
            XMLChannel.AppendChild(link);
            XMLChannel.AppendChild(description);
  
            DataTable dt = dll.getBlogDataTable();
            foreach (DataRow dr in dt.Rows)
            {
                XmlElement item = doc.CreateElement("item");

                XmlElement ItemTitle = doc.CreateElement("title");
                ItemTitle.InnerText = dr["PostTitle"].ToString();

                XmlElement ItemLink = doc.CreateElement("link");
                ItemLink.InnerText = siteDirName + "Blog/" + dr["month"].ToString() + "/" + dr["date"].ToString() + "/" + dr["year"].ToString() + "/" + dr["BlogID"].ToString() + "/" + dr["PostTitleLinks"].ToString() + extension; 

                XmlElement ItemDescription = doc.CreateElement("description");
                ItemDescription.InnerText = dr["PostContent"].ToString();

                item.AppendChild(ItemTitle);
                item.AppendChild(ItemLink);
                item.AppendChild(ItemDescription);
                XMLChannel.AppendChild(item);
            }

            doc.Save(BlogRSSFileName);
        }

        public Hashtable getPageContent(string page, string item)
        {
            Hashtable Content = new Hashtable();
            DataTable dt=dll.GetContentTableByPageAndItem(page, item);
            Content["ItemTitle"] = dt.Rows[0]["ItemTitle"].ToString();
            Content["ItemCaption"] = dt.Rows[0]["ItemCaption"].ToString();
            Content["ItemContent"] = dt.Rows[0]["ItemContent"].ToString();
            Content["ItemTitle"] = dt.Rows[0]["ImageName"].ToString();
            return Content;

        }
    }

}